package pl;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfDestination;
import com.lowagie.text.pdf.PdfNumber;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class Main {

	public Main() {
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Main m = new Main();
			FileOutputStream fos = new FileOutputStream("out.pdf");
			fos.write(m.createPdf());
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public byte[] createPdf() throws DocumentException{
		Document document = new Document();
		// step 2
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PdfWriter.getInstance(document, baos);
		// step 3
		document.open();
		// step 4
		Paragraph p = new Paragraph("Tytul filmu2",
				FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, 16));
		document.add(p);
		document.add(Chunk.NEWLINE);
		PdfPTable table = new PdfPTable(1);
//		table.addCell(Image.getInstance(KubrickMovies.PATH + movie.getPoster()));
		PdfPCell cell = new PdfPCell();
		cell.addElement(new Paragraph("Year: 9"));
		cell.addElement(new Paragraph("Duration: 88"));
		cell.addElement(new Paragraph("DVD bought on: 99" ));
		PdfPCell cell2 = new PdfPCell();
		cell.addElement(new Paragraph("Year: 9"));
		cell.addElement(new Paragraph("Duration: 88"));
		cell.addElement(new Paragraph("DVD bought on: 99" ));
		table.addCell(cell);
		table.addCell(cell2);
		document.add(table);
		
//		PdfDestination dest = new PdfDestination(PdfDestination.FIT);
//		dest.addFirst(new PdfNumber(1));
//		PdfTargetDictionary target = new PdfTargetDictionary(false);
//		target.setAdditionalPath(new PdfTargetDictionary(false));
//		Chunk chunk = new Chunk("Go to original document");
//		PdfAction action = PdfAction.gotoEmbedded(null, target, dest, false);
//		chunk.setAction(action);
//		document.add(chunk);
//		// step 5
		document.close();
		return baos.toByteArray();
	}
}
